<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="app-box">
        <el-form :inline="true" :model="params" class="demo-form-inline">
          <el-row type="flex" justify="space-between">
            <el-col :span="18">
              <el-form-item label="关键字">
                <el-input v-model="params.keyword" placeholder="根据文章标题搜索"></el-input>
              </el-form-item>
              <span class="grid-content1">
                <el-form-item label="状态">
                  <el-select v-model="params.state" placeholder="请选择">
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="0"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <template>
                    <el-button @click="rom">清除</el-button>
                    <el-button type="primary" @click="addBtn()">搜索</el-button>
                  </template>
                </el-form-item>
              </span>
            </el-col>
            <!-- 新增技巧 -->
            <el-col :span="6">
              <div class="grid-content2">
                <el-form-item>
                  <el-button type="success" class="el-icon-edit" @click="addRole">新增技巧</el-button>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
          <!-- 禁用按钮 -->
          <el-alert :title="`数据一共${total}条`" type="info" :closable="false" show-icon></el-alert>
          <!-- 表格区域 -->
          <el-table ref="singleTable" :data="tableData" highlight-current-row style="width: 100%">
            <el-table-column label="序号" type="index" width="50"></el-table-column>
            <el-table-column prop="title" label="文章标题" width="120">
              <template #default="{ row }">
                {{ row.title }}
                <a
                  v-if="row.videoURL"
                  href="#"
                  @click.prevent="openVideo(row.videoURL)"
                >
                  <i class="el-icon-film" />
                </a>
              </template>
            </el-table-column>
            <el-table-column property="visits" label="阅读数" width="120"></el-table-column>
            <el-table-column property="username" label="录入人"></el-table-column>
            <el-table-column property="createTime" label="录入时间">
              <template #default="{row}">{{createTime(row.createTime)}}</template>
            </el-table-column>
            <el-table-column property="state" label="状态">
              <template #default="{row }">{{ state(row.state) }}</template>
            </el-table-column>
            <el-table-column label="操作">
              <template v-slot="scope">
                <el-button type="text" @click="addYulan(scope.row)">预览</el-button>
                <el-button
                  type="text"
                  @click="addQy(scope.row)"
                >{{scope.row.state === 1 ? "禁用" : "启用"}}</el-button>
                <el-button
                  :disabled="scope.row.state == 1 ? false : true"
                  type="text"
                  @click="addXg(scope.row)"
                >修改</el-button>
                <el-button
                  :disabled="scope.row.state == 1 ? false : true"
                  type="text"
                  @click="editadd(scope.row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <template>
            <el-pagination
              layout="prev, pager, next, sizes,jumper"
              @size-change="handleSizeChange"
              @current-change="pageChange"
              :page-sizes="[5,10,20,30,40,50]"
              :current-page="params.page"
              :page-size="params.pagesize"
              :total="total"
              :page-count="5"
            ></el-pagination>
          </template>
        </el-form>
        <!-- 视频 -->
        <div v-if="videoURL" class="videoPlay">
          <div class="close" @click="closeVideo()">
            <span class="el-icon-close" />
          </div>
          <div class="videobox">
            <video ref="videoRef" :src="videoURL" />
          </div>
        </div>
      </div>
      <!-- 新增技巧弹框 -->
      <el-dialog title="编辑弹窗" :visible="visible" @close="closeRoleDialog">
        <!-- 表单内容 -->
        <template>
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
            <el-form-item label="文章标题" prop="title">
              <el-input v-model="roleForm.title" placeholder="请输入文章标签" />
            </el-form-item>
            <el-form-item label="文章内容" prop="articleBody">
              <!-- 使用 v-model 进行双向的数据绑定 -->
              <quill-editor v-model="roleForm.articleBody"></quill-editor>
            </el-form-item>
            <el-form-item label="视频地址" prop="videoURL">
              <el-input v-model="roleForm.videoURL" />
            </el-form-item>
          </el-form>
        </template>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center" class="footer">
          <el-button size="small" @click="revAdd">取消</el-button>
          <el-button size="small" type="primary" @click="confirm">确定</el-button>
        </el-row>
      </el-dialog>
      <!--新增预览 -->
      <el-dialog
        :model="roleData"
        title="预览文章"
        :visible="dialogVisible"
        @close="closeRoleDialog2"
        width="800px"
        :modal-append-to-body="true"
      >
        <h2>{{ roleData.title }}</h2>
        <div class="title">
          <span>{{ createTime()}}</span>
          <span>{{ roleData.username }}</span>
          <span class="el-icon-view"></span>
          <span>{{ roleData.visits }}</span>
        </div>
        <el-alert class="info1" type="info" :closable="false">
          <div v-html="roleData.articleBody" />
        </el-alert>
      </el-dialog>
      <!-- 修改文章弹框 -->
      <el-dialog title="修改弹窗" :visible="editVisible" @close="closeRoleDialog3">
        <!-- 表单内容 -->
        <template>
          <el-form ref="eidtForm" :model="eidtForm" :rules="rules" label-width="100px">
            <el-form-item label="文章标题" prop="title">
              <el-input v-model="eidtForm.title" placeholder="请输入文章标签" />
            </el-form-item>
            <el-form-item label="文章内容">
              <!-- 使用 v-model 进行双向的数据绑定 -->
              <quill-editor v-model="eidtForm.articleBody"></quill-editor>
            </el-form-item>
            <el-form-item label="视频地址" prop="videoURL">
              <el-input v-model="eidtForm.videoURL" />
            </el-form-item>
          </el-form>
        </template>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center" class="footer">
          <el-button size="small" @click="editAdd">取消</el-button>
          <el-button size="small" type="primary" @click="editCate">确定</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { list, add, changeState, update, remove } from "@/api/hmmm/articles.js";
import dayjs from "dayjs";
export default {
  data() {
    return {
      params: {
        page: 1,
        pagesize: 5,
        keyword: "",
        state: "",
      },
      // params2: {
      //   keyword: "",
      //   state: ""
      // },
      total: 0,
      // 表单
      tableData: [
        {
          title: "",
          visits: "",
          username: "",
          creatorID: ""
        }
      ],
      // 弹窗
      visible: false,
      dialogVisible: false,
      editVisible: false,
      // 预览对象
      roleData: {},
      // 文章
      roleForm: {
        title: "",
        // 文章的内容
        articleBody: "",
        videoURL: ""
      },
      // 校验
      rules: {
        title: [{ required: true, message: "请输入文章标题", trigger: "blur" }],
        articleBody: [
          { required: true, message: "请输入文章内容", trigger: "blur" }
        ]
      },
      // 视频地址
      videoURL: null,
      eidtForm: {
        title: "",
        // 文章的内容
        articleBody: "",
        videoURL: ""
      }
    };
  },
  mounted() {
    this.fetchtableData();
    this.rom()
  },
  methods: {
    async fetchtableData() {
      const res = await list(this.params);
      // console.log(res);
      this.tableData = res.data.items;
      this.total = res.data.counts;
    },
    // 分页
    pageChange(page) {
      this.params.page = page;
      this.fetchtableData();
    },
    handleSizeChange(pagesize) {
      this.params.pagesize = pagesize;
      this.fetchtableData();
    },
    // 状态
    state(type) {
      const map = {
        0: "已禁用",
        1: "已启用"
      };
      return map[type];
    },
    // 时间
    createTime(value, str = "YYYY-MM-DD HH:mm:ss") {
      return dayjs(value).format(str);
    },
    // 清除
    rom() {
      this.params = {
        page: 1,
        pagesize: 5
      };
      this.params2 = {
        keyword: "",
        state: ""
      };
      this.fetchtableData();
    },
    // 搜索
    async addBtn() {
      const res = await list({...this.params });
        this.tableData = res.data.items;
        this.total = res.data.counts;
      // if (this.params2.state === "") {
      //   this.fetchtableData();
      // } else {
      //   const res = await list({ ...this.params2, ...this.params });
      //   this.tableData = res.data.items;
      //   this.total = res.data.counts;
      // }
    },
    // 点击视频按钮播放
    openVideo(url) {
      this.videoURL = url;
      this.$nextTick(() => {
        this.$refs.videoRef.play();
      });
    },
    // 点击关闭按钮视频暂停
    closeVideo() {
      this.$refs.videoRef.pause();
      this.videoURL = null;
    },
    // 打开弹窗
    addRole() {
      this.visible = true;
    },
    // 关闭弹窗
    closeRoleDialog() {
      this.visible = false;
      this.roleForm = {
        title: "",
        videoURL: "",
        articleBody: ""
      };
      // 清除痕迹
      this.$refs.roleForm.resetFields();
    },
    // 新增技巧
    confirm() {
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          // 接口
          await add(this.roleForm);
          // 提示用户
          this.$message.success("新增成功");
          // 更新列表
          this.fetchtableData();
          // 关闭弹窗
          this.closeRoleDialog();
        }
      });
    },
    // 弹框取消
    revAdd() {
      this.closeRoleDialog();
    },
    // 打开预览
    addYulan(row) {
      this.roleData = row;
      this.dialogVisible = true;
    },
    // 关闭预览
    closeRoleDialog2() {
      this.dialogVisible = false;
    },
    // 启用
    async addQy(row) {
      if (row.state === 1) {
        row.state = 0;
      } else if (row.state === 0) {
        row.state = 1;
      }
      await changeState({ id: row.id, state: row.state });
      this.$message.success("操作成功");
    },
    // 修改
    addXg(row) {
      this.editVisible = true;
      this.eidtForm = Object.assign({}, row);
    },
    // 关闭
    closeRoleDialog3() {
      this.editVisible = false;
    },
    // 取消修改
    editAdd() {
      this.closeRoleDialog3();
    },
    // 确定修改
    editCate() {
      // 表单预校验
      this.$refs.eidtForm.validate(async valid => {
        if (valid) {
          // 发起请求
          await update(this.eidtForm);
          // 提示用户
          this.$message.success("修改成功");
          // 更新列表
          this.fetchtableData();
          // 关闭弹窗
          this.closeRoleDialog3();
        }
      });
    },
    // 删除
    async editadd(id) {
      // 询问客户
      this.$confirm("此操作将永久删除该文章, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        // 发起删除请求
        await remove({ id });
        // 提示用户
        this.$message.success("删除成功");
        // 更新列表
        this.fetchtableData();
        // 关闭弹窗
        this.closeRoleDialog3();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  background-color: #fff;
  margin: 10px;
  box-shadow: 5px;
  border-radius: 4px;
}
.el-row {
  padding: 0px 20px 0px;
}
.grid-content1 {
  margin-left: 40px;
}
.grid-content2 {
  display: flex;
  justify-content: flex-end;
}
.el-pagination {
  text-align: right;
}
.el-icon-film {
  color: #0000ff;
}
.videoPlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
}
.close {
  position: absolute;
  top: 30px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.4);
  line-height: 50px;
  text-align: center;
  padding-right: 10px;
  font-size: 20px;
  color: #fff;
}
.videobox {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 800px;
  height: 600px;
}
video {
  width: 100%;
  height: 100%;
}
::v-deep .ql-editor {
  min-height: 100px;
}
h2 {
  margin: 0;
  padding-bottom: 10px;
}
span {
  margin-left: 10px;
  margin-top: 10px;
  padding-bottom: 20px;
}
.info1 {
  border-top: 1px dashed #ccc;
  padding: 8px 0px;
}
</style>
